package com.bj58.groupbuy.action.service.impl;

import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import com.bj58.groupbuy.action.db.CommonDao;
import com.bj58.groupbuy.action.db.DBConfig;
import com.bj58.groupbuy.action.model.MSource;
import com.bj58.groupbuy.action.service.MSourceService;
import com.bj58.groupbuy.action.util.DateUtil;
import com.bj58.groupbuy.util.PathUtil;

public class MSourceServiceImpl implements MSourceService {
	private CommonDao comDao = new CommonDao();
	@Override
	public List<MSource> getAllList() throws Exception {
		String sql="select * from data_dict.t_m_source_qd";
		return comDao.getAllColsByVo(sql, MSource.class, DBConfig.db_94_data_app.getIntType());
	}

	@Override
	public List<MSource> getListByCideId(String code) throws Exception {
		String sql="select * from data_dict.t_m_source_qd where code='"+code+"';";
		return comDao.getAllColsByVo(sql, MSource.class, DBConfig.db_94_data_app.getIntType());
	}

	@Override
	public boolean dealMSourceData(List<String> ecList) throws Exception {
		HashMap<String,String> msMap=new HashMap<String,String>();
		//上传的数据
		for(String line:ecList){
			line=line.replaceAll("@", "\t");
			if(line!=null && !"".equals(line.trim())){
				String recode="";
				String[] field=line.split("\t");
				for(int i=0;i<4;i++){
					String f="";
					if(i<field.length)
						f=field[i].trim();
					recode+=f;
					if(i<3)
						recode+="\t";
				}
				msMap.put(field[0], recode);
			}
		}
		List<MSource> msList=getAllList();
		//数据库中已有的数据
		for(MSource ms:msList){
			String code=ms.getCode();
			if(!msMap.containsKey(code)){
				String line=code+"\t"+ms.getSource1()+"\t"+ms.getSource2()+"\t"+ms.getSource3();
				msMap.put(code,line);
			}
		}
		String path = PathUtil.getWFNamespaceFolder()+"/m_source_data/";
		 File dirFile = new File(path);
		if(!dirFile.exists())
			dirFile.mkdirs();
//		FileUtil.deleteDirectoryNoSefl(path);//清空目录下所有数据
		path+="m_source_"+DateUtil.getStringDate(new Date(), "yyyyMMdd_HH-mm-ss");
		FileWriter writer = new FileWriter(path);
		Iterator<String> iter=msMap.keySet().iterator();
		while(iter.hasNext()){
			String code=iter.next();
			String line=msMap.get(code);
			writer.write(line+"\n");
		}
		writer.close();
		String dropSql="drop table data_dict.t_m_source_qd;";
		String createSql="create table data_dict.t_m_source_qd(" +
				"code varchar(100) default null comment '代码标识'," +
				"source1 varchar(50) default null comment '一级来源'," +
				"source2 varchar(50) default null comment '二级来源'," +
				"source3 varchar(50) default null comment '三级来源'" +
				");";
		String loadsql="load data local infile '"+path+"'  into  table data_dict.t_m_source_qd fields terminated by '\t';";

		List<String> sqlList=new ArrayList<String>();
		sqlList.add(dropSql);
		sqlList.add(createSql);
		sqlList.add(loadsql);
		return comDao.executeSql(sqlList, DBConfig.db_94_data_app.getIntType());
	}
	
	
}
